﻿namespace FunctionalCLR.Shell.MathematicaInterface {
    public sealed class MathematicaKernel {

        private static MathematicaKernel kernel = new MathematicaKernel();

        private Wolfram.NETLink.MathKernel mathKernel;

        private MathematicaKernel() {
            this.mathKernel = new Wolfram.NETLink.MathKernel();
        }

        public static MathematicaKernel Instance {
            get {
                return kernel;
            }
        }

        public string Version {
            get {
                return kernel.Version;
            }
        }

        public object Evaluate(string expr) {
            if (mathKernel.IsComputing) {
                mathKernel.Abort();
            }

            mathKernel.Compute(expr);
            if (mathKernel.Graphics.Length > 0) {
                return (object) mathKernel.Graphics[0];
            }

            return mathKernel.Result;
        }
    }
}
